package toubiao.service.impl;

import java.rmi.UnexpectedException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import toubiao.dao.support.ResourceDaoI;
import toubiao.domain.po.manage.PoResource;
import toubiao.domain.po.manage.PoUser;
import toubiao.domain.vo.manage.Resource;
import toubiao.domain.vo.manage.User;
import toubiao.exception.CustomException;
import toubiao.service.ResourceServiceI;
import toubiao.utils.ProUtilEasyUiConvert;
import toubiao.utils.old.ProUtilPoVoConvert;
import utils.UtilBean;
import utils.UtilString;
import utils.easyui.domain.EzTree;

@Service
public class ResourceServiceImpl implements ResourceServiceI {
	
	private Logger logger=LoggerFactory.getLogger(ResourceServiceImpl.class);
	
	

	@Autowired
	private ResourceDaoI resourceDao;
	
	
	    /**
	    * @Title: getResourceByAucType
	    * @Description: 根据 aucType 属性 返回 rosourceList
	    * @param @param aucType PoResource.aucType
	    * @param @return    
	    * @return List<Resource>    
	    * @throws
	    */
	@Override
	public List<Resource> getResourceByAucType(String aucType){
		String hql="from PoResource r where r.aucType=:aucType";
		Map<String, Object> parameterMap=new HashMap<String, Object>();
		parameterMap.put("aucType", aucType);
		List<PoResource> poResourceList=resourceDao.find(hql,parameterMap);
		//logger.info("poResourceList.size() = " +poResourceList.size());
		//logger.info("ProUtilPoVoConvert.getVoList(poResourceList) = " +ProUtilPoVoConvert.getVoList(poResourceList));
		
		return ProUtilPoVoConvert.getVoListOfResource(poResourceList);
	}
	
	
	
	
	@Override
	public List<Resource> getResourceListByUser(User user) throws CustomException{
		if(user==null || user.getId()==null){
			throw new CustomException("user 为空 或者 user.id 为空");
		}
		//from Tuser t join fetch t.troles role join fetch role.tresources resource where t.id = :id"
		String hql="select resource from PoResource resource join fetch resource.roleSet role join fetch role.userSet  user where"
				+ " user.id=:userId ";
		Map<String, Object> map=new HashMap<>();
		map.put("userId", user.getId());
		List<PoResource> poResourceList=resourceDao.find(hql,map);
		logger.info("poResourceList.size() = "+poResourceList.size());
		return ProUtilPoVoConvert.getVoListOfResource(poResourceList);
	}
	
	@Override
	public List<PoResource> getResourceListByUserAndUrl(User user,String url) throws CustomException{
		if(user==null || user.getId()==null){
			throw new CustomException("user 为空 或者 user.id 为空");
		}
		if(UtilString.isNullOrEmpty(url)){
			throw new CustomException("url 为空 或者为 ''");
		}
		
		//from Tuser t join fetch t.troles role join fetch role.tresources resource where t.id = :id"
		String hql="select resource from PoResource resource join fetch resource.roleSet role join fetch role.userSet  user where"
				+ " user.id=:userId and resource.url=:url order by resource.name ";
		Map<String, Object> map=new HashMap<>();
		map.put("userId", user.getId());
		map.put("url", url);
		List<PoResource> poResourceList=resourceDao.find(hql,map);
		logger.info("poResourceList.size() = "+poResourceList.size());
		return poResourceList;
	}
	
	
	
	    /**
	     * @throws UnexpectedException 
	     * @throws InstantiationException 
	     * @throws SecurityException 
	     * @throws NoSuchFieldException 
	     * @throws IllegalAccessException 
	     * @throws IllegalArgumentException 
	    * @Title: getResourceListAsTree
	    * @Description: 将resourece组织成树的形式返回，为防止循环引用，只保留关联属性中的childrenSet
	    * @param @param poList
	    * @param @return
	    * @param @throws CustomException    
	    * @return List<Resource>    
	    * @throws
	    */
	@Override
	public List<EzTree>  getResourceListAsEzTree(List<PoResource> poList) throws CustomException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, InstantiationException, UnexpectedException{
		if(poList==null){
			return null;
		}
		List<PoResource> copyList=new ArrayList<PoResource>();
		for(PoResource source:poList){
			String[] copyProperties=new String[]{"id","name","icon","url","childrenSet"} ;
			PoResource copy=UtilBean.cycleCopyIncludeProperties(PoResource.class, source,copyProperties);
			copyList.add(copy);
		}
		return ProUtilEasyUiConvert.resource2EzTree(copyList, null);
	}
	
	
	    /* (非 Javadoc)
	    * 由于已完成untilbean  复制方法，该方法可以不使用.
	    * 
	    * @param source
	    * @return
	    * @see toubiao.service.ResourceServiceI#cycleCopyByChildrenSet(toubiao.domain.po.support.PoResource)
	    */
	@Override
	public PoResource cycleCopyByChildrenSet(PoResource source){
		if(source==null){
			return null;
		}
		PoResource copy=new PoResource();
		BeanUtils.copyProperties(source, copy, new String[]{"parentSet","jspPageSet","roleSet","childrenSet"});
		Set<PoResource> childrenSet=source.getChildrenSet();
		if(childrenSet!=null && childrenSet.size()>0){
			Set<PoResource> copyChildrenSet=new HashSet<>();
			for(PoResource p:childrenSet){
				PoResource innerCopy=cycleCopyByChildrenSet(p);
				copyChildrenSet.add(innerCopy);
			copy.setChildrenSet(copyChildrenSet);
			}
		}
		return copy;
	}
	
	
	    /**
	    * @Title: getPoResouceById
	    * @Description: TODO
	    * @param @param idString
	    * @param @return    
	    * @return PoResource    
	    * @throws
	    */
	@Override
	public PoResource getPoResouceById(int id){
		PoResource poResource=resourceDao.get(PoResource.class, id);
		return poResource;
	}
}
